package yl.hs.bmipfull.controllers.construction;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.salemanage.ICustomUserService;
import yl.hs.bmipfull.service.salemanage.ILinkerInfoService;
import yl.hs.bmipfull.service.system.IActiveService;
import yl.hs.bmipfull.service.system.IDataDictService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActiveDeviceType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.HashMap;
import java.util.Map;

/*
 * ConstructionUserController 工地用户管理
 * @category 销售管理
 * @author 谢景广
 * @date 2022-04-21
 */
@RequestMapping("/construction/user")
@Api(value = "工地系统", tags = "工地系统——用户管理")
@RestController
public class ConstructionUserController extends ControllerBase {
    @Autowired
    ILinkerInfoService service;
    @Autowired
    ICustomUserService customUserService;
    @Autowired
    IDataDictService dtSvc;
    @Autowired
    IActiveService activeService;
    @PostMapping("query/{rowNum}/{page}")
    @ApiOperation(value = "查询工地用户信息", notes = "分页查询工地用户信息" )
    public WebResult queryUserList(@PathVariable("rowNum")int rowNum, @PathVariable("page")int page, @RequestBody PageQuery query){
        try {
            var user= HsApp.getCurrent();
            var linker=service.findBySn(user.getUserSN());
            query.getQuery().put("custSn",linker.getCustSn());
            var result = service.queryPage(query, rowNum, page);
            return success("ok",result);
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "保存工地用户信息", notes = "工地用户信息")
    @RequestMapping(value = "/save", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveLinkerInfo(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);

        }

    }

    @ApiOperation(value = "保存工地用户关联合同信息", notes = "保存工地用户关联合同信息")
    @RequestMapping(value = "/saveCustomerUser", method = RequestMethod.PUT)
    public WebResult saveCustomerUser(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        String result="";
        try {
            result = customUserService.save(submitModel.getData(), submitModel.getSn());
            if (Tl.isEmpty(result)) {
                return fail("数据未保存",result);
            } else {
                return success("保存成功",result);
            }
        } catch (Exception e) {
            return error(e);
        }

    }
    @ApiOperation(value = "修改工地用户密码", notes = "修改工地用户密码")
    @RequestMapping(value = "/updatePWD/{sn}/{oldPassword}/{password}", method = RequestMethod.POST)
    public WebResult updatePWD(@PathVariable("sn")String sn, @PathVariable("oldPassword")String oldPassword,@PathVariable("password")String password) {
        Integer result;
        try {
            result = service.updatePWD(sn,oldPassword,password);
            if (result > 0) {
                return success("修改成功");
            } else {
                return fail("旧密码输入错误");
            }
        } catch (Exception e) {
            return error(e);
        }
    }
    @ApiOperation(value = "根据编号获取工地用户信息", notes = "删除工地用户信息")
    @RequestMapping(path = "/linker/{sn}", method = RequestMethod.GET)
    public WebResult getLinkerBySn(@ApiParam(value = "联系人编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String sn) {
        var user=service.findBySn(sn);
        if (user!=null) {
            return success("查询成功",user);
        } else {
            return fail("查询失败");
        }

    }
    @ApiOperation(value = "删除工地用户信息", notes = "删除工地用户信息")
    @RequestMapping(path = "/linker/{sn}", method = RequestMethod.DELETE)
    public WebResult deleteLinker(@ApiParam(value = "联系人编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "删除合同关联工地用户信息", notes = "删除合同关联工地用户信息")
    @RequestMapping(path = "/customerUser/{sn}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "联系人编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String sn) {
        if (customUserService.deleteBySn(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "查询数据字典类型", notes = "查询数据字典类型")
    @RequestMapping(value = "/item", method = RequestMethod.GET)
    public WebResult queryList() {
        try {
            Map<String,Object> data =new HashMap<String,Object>();
            var day = activeService.getAvailableDays(ActiveDeviceType.Construction);
            data.put("data_items",dtSvc.queryObjDataAll());
            data.put("available_days",day);
            return success("查询成功", data);
        } catch (Exception e) {
            return error(e);
        }
    }
}
